package pe.intix.drawback.actions


import java.text.*;

import javax.persistence.*;

import net.sf.jasperreports.engine.*
import net.sf.jasperreports.engine.data.*;

import org.openxava.actions.*;
import org.openxava.model.*;
import org.openxava.test.model.*;
import org.openxava.util.*;
import org.openxava.validators.*;

import pe.intix.drawback.model.DetalleDuaDrawback
import pe.intix.drawback.model.Dua

/**
 * @author Jeromy Altuna
 *
 */
class ReporteInsumosAction extends JasperReportBaseAction {
	
	Dua dua
	
	@Override
	public void execute() throws Exception {
		super.execute()
		addMessage("report_sucessful", "insumos_producto")
	}
	
	@Override
	protected JRDataSource getDataSource() throws Exception {
		Collection<DetalleDuaDrawback> detalle = new ArrayList<>()
		getDua().detalleDuaDrawback.each{
			if(it.proveedor != null){
				detalle.add(it)
			}
		}
		return new JRBeanCollectionDataSource(detalle)
	}

	@Override
	protected String getJRXML() throws Exception {
		return "Insumos.jrxml"
	}

	@Override
	protected Map getParameters() throws Exception {
		Messages errors = MapFacade.validate("Dua", getView().getValues())
		if (errors.contains()) throw new ValidationException(errors)
		Map parameters = new HashMap()
		parameters.put "REPORT_LOCALE", new java.util.Locale("es", "MX")
		parameters.put "idDua", getDua().idDua
		parameters.put "serie", getDua().serie
		parameters.put "fob", getDua().valorFob
		parameters.put "mercancia", getDua().mercancia
		return parameters		
	}
	
	private Dua getDua(){
		if(dua == null){
			String idDua = getView().getValueString("idDua")
			Query query = org.openxava.jpa.XPersistence.getManager().createQuery(
						"from Dua as d where d.idDua = :idDua")
			query.setParameter("idDua", idDua)		
			dua = (Dua) query.getSingleResult()
		}
		return dua
	}

}
